package com.winz.cultureapp.utils;

import android.content.Context;
import android.content.SharedPreferences;


import com.winz.cultureapp.application.DemoApplication;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.LoginUserBean;

import java.util.Map;
import java.util.Set;

/**
 * To change this template use File | Settings | File Templates.
 * 
 * @Author: lz
 * 
 */
public class SharePrefsUtil {
    private static SharePrefsUtil prefsUtil;
    public Context context;
    public SharedPreferences prefs;
    public SharedPreferences.Editor editor;

    public synchronized static SharePrefsUtil getInstance() {
        return prefsUtil;
    }

    public static void init(Context context, String prefsname, int mode) {
        prefsUtil = new SharePrefsUtil();
        prefsUtil.context = context;
        prefsUtil.prefs = prefsUtil.context.getSharedPreferences(prefsname, mode);
        prefsUtil.editor = prefsUtil.prefs.edit();
    }

    private SharePrefsUtil() {
    }


    public boolean getBoolean(String key, boolean defaultVal) {
        return this.prefs.getBoolean(key, defaultVal);
    }

    public boolean getBoolean(String key) {
        return this.prefs.getBoolean(key, false);
    }


    public String getString(String key, String defaultVal) {
        return this.prefs.getString(key, defaultVal);
    }

    public String getString(String key) {
        return this.prefs.getString(key, null);
    }

    public int getInt(String key, int defaultVal) {
        return this.prefs.getInt(key, defaultVal);
    }

    public int getInt(String key) {
        return this.prefs.getInt(key, 0);
    }


    public float getFloat(String key, float defaultVal) {
        return this.prefs.getFloat(key, defaultVal);
    }

    public float getFloat(String key) {
        return this.prefs.getFloat(key, 0f);
    }

    public long getLong(String key, long defaultVal) {
        return this.prefs.getLong(key, defaultVal);
    }

    public long getLong(String key) {
        return this.prefs.getLong(key, 0l);
    }

    public Set<String> getStringSet(String key, Set<String> defaultVal) {
        return this.prefs.getStringSet(key, defaultVal);
    }

    public Set<String> getStringSet(String key) {
        return this.prefs.getStringSet(key, null);
    }

    public Map<String, ?> getAll() {
        return this.prefs.getAll();
    }


    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public void putFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.commit();
    }

    public void putLong(String key, long value) {
        editor.putLong(key, value);
        editor.commit();
    }

    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void putStringSet(String key, Set<String> value) {
        editor.putStringSet(key, value);
        editor.commit();
    }

    /**
     * 存放用户信息 和 删除用户信息同步
     */
    public void savedUserInfo(LoginUserBean.DataBean data){
        if (data != null){
            putString(Constants.USER_INFO.TOKEN,data.token);
            if (data.user != null){
                putString(Constants.USER_INFO.USER_REAL_NAME,data.user.realname);
                putString(Constants.USER_INFO.USER_PHONE,data.user.phone);
                putString(Constants.USER_INFO.USER_NAME,data.user.username);
                putString(Constants.USER_INFO.ACCOUNT_NO,data.user.accountNo);
                putString(Constants.USER_INFO.USER_ID,data.user.userId);
                putString(Constants.USER_INFO.BIRTHDAY,data.user.birthday);
                putString(Constants.USER_INFO.HEAD_IMAGE,data.user.headImage);
                putInt(Constants.USER_INFO.USER_SEX,data.user.sex);
            }

            DemoApplication.getInstance().isLogin = true;
        }
    }
    /**
     * 删除用户信息
     */
    public void delectedUserInfo(){
        // 用户信息
        putString(Constants.USER_INFO.TOKEN,"");
        putString(Constants.USER_INFO.USER_REAL_NAME,"");
        putString(Constants.USER_INFO.USER_PHONE,"");
        putString(Constants.USER_INFO.USER_NAME,"");
        putString(Constants.USER_INFO.ACCOUNT_NO,"");
        putString(Constants.USER_INFO.USER_ID,"");
        putString(Constants.USER_INFO.BIRTHDAY,"");
        putString(Constants.USER_INFO.HEAD_IMAGE,"");
        putInt(Constants.USER_INFO.USER_SEX,0);
        DemoApplication.getInstance().isLogin = false;
        SharePrefsUtil.getInstance().putBoolean(Constants.USER_INFO.ISADMIN,false);
    }
}